package cn.wolfcode.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.query.AppointmentQueryObject;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.statusenum.AppointmentStatusEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") AppointmentQueryObject qo, Model model) {
        System.out.println(appointmentService.selectAll());
        PageInfo<Appointment> pageInfo = appointmentService.list(qo);

        model.addAttribute("result", pageInfo);

        AppointmentStatusEnum[] values = AppointmentStatusEnum.values();
        model.addAttribute("statuses", values);

        List<SystemDictionaryItem> systemDictionaryItemList = systemDictionaryItemService.selectByTypeId(1);
        model.addAttribute("categorys", systemDictionaryItemList);

        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses", businesses);

        return "appointment/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId() == null) {
            appointment.setStatus(0);
            appointment.setCreate_time(new Date());
            String date = DateUtil.format(new Date(), "yyyyMMddHHmm");
            String rand = RandomUtil.randomNumbers(5);
            appointment.setAno(date + rand);
            appointmentService.insert(appointment);
        } else {
            appointmentService.updateByPrimaryKey(appointment);
        }
        return "redirect:/appointment/list";
    }

    @RequestMapping("/saveOrUpdate_async")
    @ResponseBody
    public JsonResult saveOrUpdate_async(Appointment appointment) {
        if (appointment.getId() == null) {
            appointment.setStatus(0);
            appointment.setCreate_time(new Date());
            String date = DateUtil.format(new Date(), "yyyyMMddHHmm");
            String rand = RandomUtil.randomNumbers(5);
            appointment.setAno(date + rand);
            appointmentService.insert(appointment);
        } else {
            appointmentService.updateByPrimaryKey(appointment);
        }
        return JsonResult.success(null);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Appointment appointment,Long id) {

        if (appointment.getStatus()==4){
            appointmentService.deleteByPrimaryKey(id);
            return JsonResult.success(null);
        }
        return JsonResult.error("仅废弃状态可删除！");
    }
}
